package com.example.constellation;

import android.widget.RadioGroup;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import com.example.constellation.bean.StarInfoBean;
import com.example.constellation.luckfrag.LuckFragment;
import com.example.constellation.mefrag.MeFragment;
import com.example.constellation.partnerfrag.PartnerFragment;
import com.example.constellation.starfrag.StarFragment;
import com.example.constellation.util.AssetsUtil;
import com.google.gson.Gson;

public class MainActivity extends AppCompatActivity {
    RadioGroup mainrg;
    //声明按钮对应的Fragment对象
    Fragment starFrag,luckFrag,partnerFrag,meFrag;
    private FragmentManager manager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainrg=findViewById(R.id.main_rg);
        //监听
        mainrg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                FragmentTransaction transaction=manager.beginTransaction();
                switch (i){
                    case R.id.main_rb_star:
                        System.out.println("星座");
                        transaction.hide(luckFrag);
                        transaction.hide(partnerFrag);
                        transaction.hide(meFrag);
                        transaction.show(starFrag);
                        transaction.commit();
                        break;
                    case R.id.main_rb_parnter:
                        System.out.println("配对");
                        transaction.hide(luckFrag);
                        transaction.hide(starFrag);
                        transaction.hide(meFrag);
                        transaction.show(partnerFrag);
                        transaction.commit();
                        break;
                    case R.id.main_rb_luck:
                        System.out.println("运势");
                        transaction.hide(starFrag);
                        transaction.hide(partnerFrag);
                        transaction.hide(meFrag);
                        transaction.show(luckFrag);
                        transaction.commit();
                        break;
                    case R.id.main_rb_me:
                        System.out.println("我的");
                        transaction.hide(luckFrag);
                        transaction.hide(partnerFrag);
                        transaction.hide(starFrag);
                        transaction.show(meFrag);
                        transaction.commit();
                        break;
                }
            }
        });
        //加载星座相关数据  /assets/xzcontent/xzcontent.json
        StarInfoBean infoBean=loadData();
        Bundle bundle=new Bundle();
        bundle.putSerializable("info",infoBean);

        //new 对象
        starFrag=new StarFragment();
        starFrag.setArguments(bundle);
        luckFrag=new LuckFragment();
        luckFrag.setArguments(bundle);
        partnerFrag=new PartnerFragment();
        partnerFrag.setArguments(bundle);
        meFrag=new MeFragment();
        meFrag.setArguments(bundle);
        //将Fragment进行动态加载,一起加载到布局中  replace         add/hide/show（添加,隐藏,显示）
        addFragmentPage();
    }

    /**
     * 读取assets文件夹下的xzcontent.json星座信息
     */
    private StarInfoBean loadData(){
        String json= AssetsUtil.getJsonFromAssets(this,"xzcontent/xzcontent.json");
        Gson gson=new Gson();
        StarInfoBean infoBean =gson.fromJson(json,StarInfoBean.class);
        AssetsUtil.saveBitmapFromAssets(this,infoBean);
        return infoBean;
    }
    //add显示的界面
    private void addFragmentPage(){
        //创建管理者对象
        manager=getSupportFragmentManager();
        //创建碎片处理事务的对象
        FragmentTransaction transaction=manager.beginTransaction();
        //将四个fragment统一添加到布局中
        transaction.add(R.id.main_layout_center,starFrag);
        transaction.add(R.id.main_layout_center,partnerFrag);
        transaction.add(R.id.main_layout_center,luckFrag);
        transaction.add(R.id.main_layout_center,meFrag);
        //隐藏后面三个
        transaction.hide(partnerFrag);
        transaction.hide(luckFrag);
        transaction.hide(meFrag);
        //提交改变后的事务
        transaction.commit();
    }
}
